﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using WalkingTec.Mvvm.Core;
using System.Text.Json.Serialization;
using nERP.Model;
using nERP.Model.So;
using nERP.Model.Wms;
using nERP.Model.Fin;

namespace nERP.Model.Bas
{
    /// <summary>
    /// 客户
    /// </summary>
	[Table("Customer")]
    [SoftKey(nameof(Customer.CustomerCode))]
    [Display(Name = "_Model.Customer")]
    public class Customer : BasePoco,IPersistPoco,ITenant
    {
        [Display(Name = "_Model._Customer._CustomerCode")]
        [StringLength(32, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("客户编码")]
        [Required(ErrorMessage = "Validate.{0}required")]
        [CanNotEdit]
        public string CustomerCode { get; set; }
        [Display(Name = "_Model._Customer._CustomerName")]
        [StringLength(64, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("客户名称")]
        [Required(ErrorMessage = "Validate.{0}required")]
        public string CustomerName { get; set; }
        [Display(Name = "_Model._Customer._ContactPerson")]
        [StringLength(64, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("联系人")]
        public string ContactPerson { get; set; }
        [Display(Name = "_Model._Customer._PhoneNumber")]
        [StringLength(32, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("手机号码")]
        [RegularExpression("^[1][345789][0-9]{9}$", ErrorMessage = "Validate.{0}formaterror")]
        public string PhoneNumber { get; set; }
        [Display(Name = "_Model._Customer._Email")]
        [StringLength(32, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("邮箱")]
        [RegularExpression("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", ErrorMessage = "Validate.{0}formaterror")]
        public string Email { get; set; }
        [Display(Name = "_Model._Customer._Faxing")]
        [StringLength(32, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("传真")]
        public string Faxing { get; set; }
        [Display(Name = "_Model._Customer._BeginCollect")]
        [Comment("期初应收")]
        public decimal? BeginCollect { get; set; }
        [Display(Name = "_Model._Customer._EndCollect")]
        [Comment("期末应收")]
        public decimal? EndCollect { get; set; }
        [Display(Name = "_Model._Customer._TaxtRate")]
        [Comment("税率")]
        public decimal? TaxtRate { get; set; }
        [Display(Name = "_Model._Customer._Address")]
        [StringLength(256, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("地址")]
        public string Address { get; set; }
        [Display(Name = "_Model._Customer._Bank")]
        [StringLength(256, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("开户行")]
        public string Bank { get; set; }
        [Display(Name = "_Model._Customer._TaxpayerNumber")]
        [StringLength(32, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("纳税人识别号")]
        public string TaxpayerNumber { get; set; }
        [Display(Name = "_Model._Customer._BankAccount")]
        [StringLength(32, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("银行账号")]
        public string BankAccount { get; set; }
        [Display(Name = "_Model._Customer._LandlinePhone")]
        [StringLength(32, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("固定电话")]
        [RegularExpression("^[-0-9\\s]{8,30}$", ErrorMessage = "Validate.{0}formaterror")]
        public string LandlinePhone { get; set; }
        [Display(Name = "_Model._Customer._Remark")]
        [StringLength(256, ErrorMessage = "Validate.{0}stringmax{1}")]
        [Comment("备注")]
        public string Remark { get; set; }
        [Display(Name = "_Model._Customer._CustomerFile")]
        [Comment("客户附件")]
        public List<CustomerCustomerFile> CustomerFile { get; set; }
        [Display(Name = "_Model._Sale._Customer")]
        [InverseProperty("Customer")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.So.Sale.CustomerId))]
        public List<Sale> Sale_Customer { get; set; }
        [Display(Name = "_Model._SaleReturn._Customer")]
        [InverseProperty("Customer")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.So.SaleReturn.CustomerId))]
        public List<SaleReturn> SaleReturn_Customer { get; set; }
        [Display(Name = "_Model._SaleOutWarehouse._Customer")]
        [InverseProperty("Customer")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.Wms.SaleOutWarehouse.CustomerId))]
        public List<SaleOutWarehouse> SaleOutWarehouse_Customer { get; set; }
        [Display(Name = "_Model._SaleReturnInWarehouse._Customer")]
        [InverseProperty("Customer")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.Wms.SaleReturnInWarehouse.CustomerId))]
        public List<SaleReturnInWarehouse> SaleReturnInWarehouse_Customer { get; set; }
        [Display(Name = "_Model._Receivable._Customer")]
        [InverseProperty("Customer")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.Fin.Receivable.CustomerId))]
        public List<Receivable> Receivable_Customer { get; set; }
        [Display(Name = "_Model._OtherIncome._Customer")]
        [InverseProperty("Customer")]
        [NotMapped]
        [SoftFK(nameof(nERP.Model.Fin.OtherIncome.CustomerId))]
        public List<OtherIncome> OtherIncome_Customer { get; set; }
        [Display(Name = "_Model._Customer._IsValid")]
        [Comment("是否有效")]
        [Required(ErrorMessage = "Validate.{0}required")]
        public bool IsValid { get; set; } = true;
        [StringLength(50, ErrorMessage = "Validate.{0}stringmax{1}")]
        public string TenantCode { get; set; }

    }
    public class CustomerCustomerFile : TopBasePoco, ISubFile
    {
        public Guid CustomerId { get; set; }
        public Customer Customer { get; set; }
        public Guid FileId { get; set; }
        public FileAttachment File { get; set; }
        public int Order { get; set; }
    }

}
